home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / python-gnome2 / examples / bonobo / bonoboui / hello.py < prev   
Encoding:
Python Source  |  2009-03-14  |  3.9 KB  |  145 lines

  1. #!/usr/bin/env python
  2. #
  3. # hello.py
  4. #
  5. # A hello world application using the Bonobo UI handler
  6. #
  7. # Original Authors:
  8. #      Michael Meeks    <michael@ximian.com>
  9. #      Murray Cumming   <murrayc@usa.net>
  10. #      Havoc Pennington <hp@redhat.com>
  11. #
  12. # Converted to Python by:
  13. #      Johan Dahlin     <jdahlin@telia.com>
  14. #
  15.     
  16. import sys
  17. import bonobo
  18. import bonobo.ui
  19. import gtk
  20.  
  21. HELLO_UI_XML = "Bonobo_Sample_Hello.xml"
  22.  
  23. # Keep a list of all open application windows
  24. app_list = []
  25.  
  26. def strreverse (text):
  27.     l = list (text)
  28.     l.reverse ()
  29.     return ''.join (l)
  30.  
  31. def show_nothing_dialog (widget):
  32.     dialog = gtk.MessageDialog (widget,
  33.                                 gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
  34.                 gtk.MESSAGE_INFO, gtk.BUTTONS_OK,
  35.                 'This does nothing; it is only a demonstration')
  36.     dialog.run ()
  37.     dialog.destroy ()
  38.     
  39. def hello_on_menu_file_new (uic, verbname, win):
  40.     hello = hello_new ()
  41.     hello.show_all ()
  42.     
  43. def hello_on_menu_file_open (uic, verbname, win):
  44.     show_nothing_dialog (win)
  45.     
  46. def hello_on_menu_file_save (uic, verbname, win):
  47.     show_nothing_dialog (win)
  48.     
  49. def hello_on_menu_file_saveas (uic, verbname, win):
  50.     show_nothing_dialog (win)
  51.     
  52. def hello_on_menu_file_exit (uic, verbname, win):
  53.     sys.exit (0)
  54.  
  55. def hello_on_menu_file_close (uic, verbname, win):
  56.     app_list.remove (app)
  57.     app.destroy ()
  58.     if not app_list:
  59.     hello_on_menu_file_exit (uic, verbname, win)
  60.  
  61. def hello_on_menu_edit_undo (uic, verbname, win):
  62.     show_nothing_dialog (win)    
  63.     
  64. def hello_on_menu_edit_redo (uic, verbname, win):
  65.     show_nothing_dialog (win)        
  66.     
  67. def hello_on_menu_help_about (uic, verbname, win):
  68.     dialog = gtk.MessageDialog (win,
  69.                                 gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
  70.                 gtk.MESSAGE_INFO, gtk.BUTTONS_OK,
  71.                 'BonoboUI-Hello')
  72.     dialog.run ()
  73.     dialog.destroy ()    
  74.     
  75. def hello_on_button_click (w, label):
  76.     text = label.get_text ()
  77.     label.set_text (strreverse (text))
  78.  
  79. # These verb names are standard, see libonobobui/doc/std-ui.xml
  80. # to find a list of standard verb names.
  81. # The menu items are specified in Bonobo_Sample_Hello.xml and
  82. # given names which map to these verbs here.
  83.  
  84. hello_verbs = [
  85.     ('FileNew',    hello_on_menu_file_new),
  86.     ('FileOpen',   hello_on_menu_file_open),
  87.     ('FileSave',   hello_on_menu_file_save),
  88.     ('FileSaveAs', hello_on_menu_file_saveas),
  89.     ('FileClose',  hello_on_menu_file_close),
  90.     ('FileExit',   hello_on_menu_file_exit),
  91.     ('EditUndo',   hello_on_menu_edit_undo),
  92.     ('EditRedo',   hello_on_menu_edit_redo),    
  93.     ('HelpAbout',  hello_on_menu_help_about)
  94. ]
  95.     
  96. def hello_create_main_window ():
  97.     window = bonobo.ui.Window ('Title', 'test')
  98.     window.show_all ()
  99.  
  100.     ui_container = window.get_ui_container ()
  101.     engine = window.get_ui_engine ()
  102.     engine.config_set_path ('/hello-app/UIConfig/kvps')
  103.     ui_component = bonobo.ui.Component ('test')
  104.     ui_component.set_container (ui_container.corba_objref ())
  105.  
  106.     bonobo.ui.util_set_ui (ui_component, '',
  107.                            HELLO_UI_XML,
  108.                    'bonobo-hello')
  109.                
  110.     ui_component.add_verb_list (hello_verbs, window)
  111.     return window
  112.  
  113. def delete_event_cb (window, event):
  114.     return gtk.TRUE
  115.  
  116. def hello_new ():
  117.     win = hello_create_main_window ()
  118.     
  119.     button = gtk.Button ()
  120.     button.set_border_width (10)
  121.     
  122.     label = gtk.Label ('Hello World')
  123.     button.add (label)
  124.     button.connect ('clicked', hello_on_button_click, label)
  125.     
  126.     win.set_size_request (250, 350)
  127.     win.set_resizable (gtk.TRUE)
  128.     win.set_property ('allow-shrink', gtk.FALSE)
  129.     
  130.     frame = gtk.Frame ()
  131.     frame.set_shadow_type (gtk.SHADOW_IN)
  132.     frame.add (button)
  133.     win.set_contents (frame)
  134.     
  135.     win.connect ('delete_event', delete_event_cb)
  136.     
  137.     app_list.append (win)
  138.     
  139.     return win
  140.  
  141. if __name__ == '__main__':
  142.     app = hello_new ()
  143.     app.show_all ()
  144.     bonobo.main ()
  145.